using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.Common;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;

using SLIITPro.Core;
using SLIITPro.Data.Extension;
using SLIITPro.Data.Entity;

namespace SLIITPro.Data.Providers
{
    public class UserMembershipProvider : MembershipProvider
    {
        private string connStringName;
        private string applicationName;
        private MembershipPasswordFormat passwordFormat;
        private UserExtension _userExtension;
        private Datamanager _datamanager;
        private Users _users;

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            if (config["connectionStringName"] == null)
            {
                config["connectionStringName"] = "SLIITProConnectionString";
            }
            connStringName = config["connectionStringName"];
            config.Remove("connectionStringName");

            if (config["applicationName"] == null)
            {
                config["applicationName"] = "SLIITPro";
            }
            applicationName = config["applicationName"];
            config.Remove("applicationName");
        }

        public override string ApplicationName
        {
            get { return applicationName; }
            set { throw new System.NotImplementedException(); }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new System.NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new System.NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new System.NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new System.NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new System.NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new System.NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new System.NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new System.NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new System.NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new System.NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new System.NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            _datamanager = new Datamanager();
            MembershipUser user = null;

            var query = SLIITPro.Data.Properties.Resources.MProviderGetUserQuery;
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@1", SqlDbType.NVarChar) { Value = username };
            DataRow row = _datamanager.ExecuteQuery(query, "Users", parameters).Rows[0];
            if (row != null)
            {
                user = GetMembershipUser(row[1].ToString(), row[2].ToString(), row[3].ToString());
            }
            return user;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return GetUser(providerUserKey.ToString(), userIsOnline);
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new System.NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new System.NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new System.NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new System.NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new System.NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new System.NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new System.NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new System.NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new System.NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new System.NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new System.NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new System.NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            throw new System.NotImplementedException();
        }

        private MembershipUser GetMembershipUser(string firstname, string lastname, string email)
        {
            var user = new MembershipUser(
                                    Name,                       // Provider name
                                    email, // Username
                                    lastname,                   // providerUserKey
                                    email,                      // Email
                                    firstname + " " + lastname,               // passwordQuestion
                                    String.Empty,               // Comment
                                    true,                       // isApproved
                                    false,                      // isLockedOut
                                    DateTime.Now,               // creationDate
                                    DateTime.Now,                  // lastLoginDate
                                    DateTime.Now,               // lastActivityDate
                                    DateTime.Now,               // lastPasswordChangedDate
                                    new DateTime(1980, 1, 1)    // lastLockoutDate
                                );
            return user;
        }
    }
}